package com.sander.guardmanagement.ui.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.sander.guardmanagement.R;
import com.sander.guardmanagement.http.BaseCallback;
import com.sander.guardmanagement.http.OkHttpHelper;
import com.sander.guardmanagement.http.UserGetUserInfo;
import com.sander.guardmanagement.http.UserInfomationchange;
import com.sander.guardmanagement.response.ResponseModel;
import com.sander.guardmanagement.response.ResponseUserLogin;
import com.sander.guardmanagement.response.ResponseUserLoginData;
import com.sander.guardmanagement.until.UserInfoManager;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

public class SDPersonalInformationActivity extends AppCompatActivity {

    public EditText nameinputView;
    public EditText ageinputView;

    public EditText xueliinputview;

    public Button sex_man_button ;
    public Button sex_woman_button;
    public Button hunying_yes_button;
    public Button hunyin_not_button;
    String sex_string = "1";
    String hunyin_string = "0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sdpersonal_information);
        initCallBack();
        initView();
        setsexmode(true);
        sethunyingModel(false);
        initData();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

    }
    private void initCallBack(){
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.callbackLayout);
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }



    private void initView(){
        Button button = (Button) findViewById(R.id.sureqpplycooperation);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                suredata();
            }
        });
        nameinputView = (EditText) findViewById(R.id.nameinputview);
        ageinputView = (EditText) findViewById(R.id.ageinputview);
        xueliinputview = (EditText) findViewById(R.id.xueliinputview);

        sex_man_button = (Button) findViewById(R.id.sexmanbutton);
        sex_woman_button = (Button) findViewById(R.id.sexwomanbutton);
        sex_man_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               setsexmode(true);
            }
        });
        sex_woman_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setsexmode(false);
            }
        });

        hunyin_not_button = (Button)findViewById(R.id.weihunbutton);
        hunying_yes_button = (Button)findViewById(R.id.yihunbutton);
        hunyin_not_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sethunyingModel(false);
            }
        });
        hunying_yes_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sethunyingModel(true);
            }
        });


        UserInfoManager userInfoManager = UserInfoManager.sharedManager();
       configViewData(userInfoManager.getUserInfo());

    }

    public void configViewData(ResponseUserLoginData userinfo){
        if (!TextUtils.isEmpty(userinfo.getUsername())){
            nameinputView.setText(userinfo.getUsername());
        }
        if (userinfo.getAge() != 0){
            ageinputView.setText(userinfo.getAge() + "");
        }
        if (userinfo.getGender() == 1){
            setsexmode(true);
        }else{
            setsexmode(false);
        }
        if (userinfo.getMarry() == 0){
            sethunyingModel(false);
        }else{
            sethunyingModel(true);
        }
        if (!TextUtils.isEmpty((userinfo.getXueli()))){
            xueliinputview.setText(userinfo.getXueli());
        }

    }

    public void setsexmode(boolean model){
        if (model){
            sex_string = "1";
        }else{
            sex_string = "0";
        }
        sex_man_button.setSelected(model);
        sex_woman_button.setSelected(!model);
    }
    public void sethunyingModel(boolean model){
        if (model){
            hunyin_string = "1";
        }else{
            hunyin_string = "0";
        }
        hunying_yes_button.setSelected(model);
        hunyin_not_button.setSelected(!model);
    }

    public void initData(){

        UserGetUserInfo userGetUserInfo = new UserGetUserInfo();
        userGetUserInfo.uid = UserInfoManager.sharedManager().getUserInfo().getId() + "";
        OkHttpHelper.getinstance().post(userGetUserInfo.requestUrl(), userGetUserInfo.getValueOfClas(), new BaseCallback<ResponseUserLogin>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, ResponseUserLogin o) {
                if (o.getCode() == 0){
                    ResponseUserLoginData userInfo = o.getData();
                    configViewData(userInfo);
                }
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });
    }

    public void suredata(){
        String name_string = nameinputView.getText().toString();
        String age_string =ageinputView.getText().toString();

        String xueli_string = xueliinputview.getText().toString();
        if (TextUtils.isEmpty(name_string)) showToastMessage("姓名不能为空");
        if (TextUtils.isEmpty(age_string)) showToastMessage("年龄不能为空");
        if (TextUtils.isEmpty(xueli_string)) showToastMessage("学历不能为空");
        UserInfomationchange userInfomationchange = new UserInfomationchange();
        userInfomationchange.uid =UserInfoManager.sharedManager().getUserInfo().getId() + "";
        userInfomationchange.username = name_string;
        userInfomationchange.age = age_string;
        String gender_string = sex_string;
        String marray_string = hunyin_string;

        userInfomationchange.gender = gender_string;
        userInfomationchange.marry =marray_string;
        userInfomationchange.xueli = xueli_string;

        OkHttpHelper.getinstance().post(userInfomationchange.requestUrl(), userInfomationchange.getValueOfClas(), new BaseCallback<ResponseModel>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, ResponseModel o) {
                if (o.getCode() == 0){
                    finish();
                }else{
                    showToastMessage(o.getMsg());
                }
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });


    }

    public void showToastMessage(String message){
        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();

    }

}
